package com.guigu.xm.yy.controller;

import com.guigu.xm.advice.Interceptor;
import com.guigu.xm.pojo.*;
import com.guigu.xm.yy.service.impl.MApplyServiceImpl;
import com.guigu.xm.yy.service.impl.YyDfileServiceimpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cha")
public class YyDFileController {
        @Autowired(required = false)
        YyDfileServiceimpl yyDfileService;
        @Autowired(required = false)
        MApplyServiceImpl ms;
        static int sutoal=0;
        static int allsutoalW=0;
        @PostMapping
        @ResponseBody
        public PageVo fenyeList(@RequestBody Map<String,String> map, DFile dFile){
            String firstKindName = map.get("firstKindName");
            String secondKindName = map.get("secondKindName");
            String thirdKindName = map.get("thirdKindName");
            String register_time1 = map.get("register_time1");
            String register_time2 = map.get("register_time2");
            dFile.setFirstKindName(firstKindName);
            dFile.setSecondKindName(secondKindName);
            dFile.setThirdKindName(thirdKindName);
            dFile.setRegister_time1(register_time1);
            dFile.setRegister_time2(register_time2);
            PageVo pageVo = yyDfileService.fenyeaList(Integer.valueOf(map.get("page")), Integer.valueOf(map.get("pageNum")), dFile);
            return pageVo;
        }
        @GetMapping(value = "xx/{productId}")
        @ResponseBody
        public DFile chacpid(@PathVariable("productId") String productId){
            DFile chacpid = yyDfileService.chacpid(productId);
            return chacpid;
        }
        @PostMapping(value = "up/{productId}")
        @ResponseBody
        public int update(@PathVariable("productId") String productId){
            int update = yyDfileService.update(productId);
            return update;
        }
        @PostMapping("/add")
        @ResponseBody
        public int add(@RequestBody MDesignProcedure mDesignProcedure){
            String s = yyDfileService.selId();
            int substring = Integer.parseInt(s.substring(11, 15));
            int i = substring+ 10000 + 1;
            String ss = String.valueOf(i);
            String lsh = ss.substring(1,5);
            Date date= new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String xx = sdf.format(date);
            mDesignProcedure.setFirstKindId("1");
            mDesignProcedure.setFirstKindName("电子");
            mDesignProcedure.setSecondKindId("2");
            mDesignProcedure.setSecondKindName("计算机");
            mDesignProcedure.setThirdKindId("3");
            mDesignProcedure.setThirdKindName("服务器");
            mDesignProcedure.setDesignId("201"+xx+""+lsh+"");
            mDesignProcedure.setCheckTag("S001-0");
            mDesignProcedure.setChanger("B002-0");
            mDesignProcedure.setDesignModuleTag("G002-0");
            mDesignProcedure.setDesignModuleChangeTag("G003-0");
            System.out.println(mDesignProcedure);
           return yyDfileService.add(mDesignProcedure);
        }
        @PostMapping("/add1")
        @ResponseBody
        public int add1(@RequestBody MDesignProcedureDetails mDesignProcedureDetails){
            System.out.println(mDesignProcedureDetails);
            mDesignProcedureDetails.setDesignModuleTag1("D002-0");
            mDesignProcedureDetails.setDesignModuleChangeTag("D003-0");
            return yyDfileService.add1(mDesignProcedureDetails);
        }
    @PostMapping("/sh")
    @ResponseBody
    public PageVo fenyeList1(@RequestBody Map<String,String> map, MDesignProcedure mDesignProcedure){
        PageVo pageVo = yyDfileService.fenyeaList1(Integer.valueOf(map.get("page")), Integer.valueOf(map.get("pageNum")), mDesignProcedure);
        return pageVo;
    }
    @GetMapping(value = "sj/{productId}")
    @ResponseBody
    public List<MDesignProcedure> cll(@PathVariable("productId") String productId){
        System.out.println(productId);
        List<MDesignProcedure> dbcha = yyDfileService.dbcha(productId);
        System.out.println(dbcha);
        return dbcha;
    }
    @PostMapping("/up")
    @ResponseBody
    public int update1(@RequestBody MDesignProcedure mDesignProcedure){
        int update = yyDfileService.update(mDesignProcedure);
        return update;
    }

    @PostMapping("/fy")
    @ResponseBody
    public PageVo fenyeall(@RequestBody Map<String,String> map,MDesignProcedure mDesignProcedure){
        String firstKindName = map.get("firstKindName");
        String secondKindName = map.get("secondKindName");
        String thirdKindName = map.get("thirdKindName");
        String productName = map.get("productName");
        String checkTag = map.get("checkTag");
        String register_time1 = map.get("register_time1");
        String register_time2 = map.get("register_time2");
        mDesignProcedure.setFirstKindName(firstKindName);
        mDesignProcedure.setSecondKindName(secondKindName);
        mDesignProcedure.setThirdKindName(thirdKindName);
        mDesignProcedure.setProductName(productName);
        mDesignProcedure.setCheckTag(checkTag);
        mDesignProcedure.setRegister_time1(register_time1);
        mDesignProcedure.setRegister_time2(register_time2);
        PageVo cha = yyDfileService.cha(Integer.valueOf(map.get("page")), Integer.valueOf(map.get("pageNum")), mDesignProcedure);
        return cha;
    }
    @GetMapping("/cx")
    @ResponseBody
    public List<MDesignProcedure> all(){
            return yyDfileService.xlk();
    }
    @PostMapping("/wl")
    @ResponseBody
    public PageVo list(@RequestBody Map<String,Integer> map){
        PageVo list = yyDfileService.list(map.get("page"), map.get("pageNum"));
        return list;
    }
    @PostMapping(value = "wg/{designId}")
    @ResponseBody
    public List<MDesignProcedure> alsl(@PathVariable("designId") String designId){
        List<MDesignProcedure> slal = yyDfileService.slal(designId);
        return slal;
    }
    @GetMapping(value = "sj1/{product_id}")
    @ResponseBody
    public List<DModule> all1(@PathVariable("product_id") String product_id){
        System.out.println(product_id);
        List<DModule> all = yyDfileService.all(product_id);
        System.out.println(all);
        return all;
    }
    @PostMapping("/ky")
    @ResponseBody
    public int xg(@RequestBody DModuleDetails dModuleDetails){
        System.out.println(dModuleDetails);
        int xg = yyDfileService.xg(dModuleDetails);
        return xg;
    }
    @PostMapping(value = "zt/{mid}")
    @ResponseBody
    public int update11(@PathVariable("mid") int mid){
        int update = yyDfileService.update(mid);
        return update;
    }

    @PostMapping("/xg")
    @ResponseBody
    public int updaatexj(@RequestBody MDesignProcedureDetails mDesignProcedureDetails){
        System.out.println(mDesignProcedureDetails);
        int xgxj = yyDfileService.xgxj(mDesignProcedureDetails);
        return xgxj;
    }
    @PostMapping("/addw")
    @ResponseBody
    public int addd(@RequestBody MDesignProcedureModuleDetails mDesignProcedureModuleDetails){
        int i = yyDfileService.add2(mDesignProcedureModuleDetails);
        return i;
    }
    @PostMapping(value = "upz/{procedureId}")
    @ResponseBody
    public int upzt(@PathVariable("procedureId") String procedureId){
        int upzt = yyDfileService.upzt(procedureId);
        return upzt;
    }
    @PostMapping("/wlsh")
    @ResponseBody
    public PageVo list1(@RequestBody Map<String,Integer> map){
        PageVo list = yyDfileService.list11(map.get("page"), map.get("pageNum"));
        return list;
    }
    @GetMapping(value = "csh/{productId}")
    @ResponseBody
    public List<MDesignProcedure> allzz(@PathVariable("productId") String productId){
        List<MDesignProcedure> dbcha = yyDfileService.dbcha(productId);
        return dbcha;
    }
    @PostMapping("/swl")
    @ResponseBody
    public int upwl1(@RequestBody MDesignProcedure mDesignProcedure){
           return yyDfileService.upshwl(mDesignProcedure);
    }
    @PostMapping("/fy1")
    @ResponseBody
    public PageVo fenyeall1(@RequestBody Map<String,String> map,MDesignProcedure mDesignProcedure){
        System.out.println(map);
        String firstKindName = map.get("firstKindName");
        String secondKindName = map.get("secondKindName");
        String thirdKindName = map.get("thirdKindName");
        String productName = map.get("productName");
        String designModuleTag = map.get("designModuleTag");
        String register_time1 = map.get("register_time1");
        String register_time2 = map.get("register_time2");
        mDesignProcedure.setFirstKindName(firstKindName);
        mDesignProcedure.setSecondKindName(secondKindName);
        mDesignProcedure.setThirdKindName(thirdKindName);
        mDesignProcedure.setProductName(productName);
        mDesignProcedure.setDesignModuleTag(designModuleTag);
        mDesignProcedure.setRegister_time1(register_time1);
        mDesignProcedure.setRegister_time2(register_time2);
        PageVo cha = yyDfileService.cha1(Integer.valueOf(map.get("page")), Integer.valueOf(map.get("pageNum")), mDesignProcedure);
        return cha;
    }

    //生产调度罗庆分页查询
    @PostMapping("/allDan")
    @ResponseBody
    public PageVo<MApply> allDan(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        return ms.allJihuaDan(page,num);
    }

    //查找所有得工序和物料
    @RequestMapping("/allGxu")
    @ResponseBody
    public List<MDesignProcedureDetails> allGxu(@RequestBody String parent_id){
        System.out.println(parent_id);
        String substring = parent_id.substring(0, parent_id.length() - 1);
        List<MDesignProcedure> mDesignProcedures = yyDfileService.allGxu(substring);
        List<MDesignProcedureDetails> mDesignProcedureDetailsList= mDesignProcedures.get(0).getMDesignProcedureDetailsList();
        return mDesignProcedureDetailsList;
    }

    @RequestMapping("/allWuliao")
    @ResponseBody
    public List<MDesignProcedureModuleDetails> allWuliao(@RequestBody String parent_id){
        return yyDfileService.allWuliao(parent_id);
    }
    public int gxwuxj(String gxbh){
        List<MDesignProcedureModuleDetails> mDesignProcedureModuleDetails = yyDfileService.allWuliao(gxbh);
        int aa=0;
        for (int i=0;i<mDesignProcedureModuleDetails.size();i++){
            aa+=mDesignProcedureModuleDetails.get(i).getSubtotal();
        }
        return aa;
    }
    @RequestMapping("/AddMp")
    @ResponseBody
    public int addMp(String cpid,String gxbh,String gxname,String gsj,String gxj,String gdj){
        MProcedure mProcedure=new MProcedure();
        mProcedure.setParentId(cpid);
        mProcedure.setDetailsInt(Integer.parseInt(gxbh));
        mProcedure.setProcedureName(gxname);
        mProcedure.setLabourHourAmount(Integer.parseInt(gsj));
        mProcedure.setSubtotal(Integer.parseInt(gxj));
        mProcedure.setCostPrice(Integer.parseInt(gdj));
        mProcedure.setDemandAmount(1);
        sutoal = gxwuxj(gxbh);
        System.out.println(allsutoalW+=sutoal);
        mProcedure.setModuleSubtotal(sutoal);
        mProcedure.setProcedureFinishTag("G004-0");
        return yyDfileService.insertMP(mProcedure);
    }
    @RequestMapping("/AddMpM")
    @ResponseBody
    public int AddMpm(String gxbh){
        List<MDesignProcedureModuleDetails> mDesignProcedureModuleDetails = yyDfileService.allWuliao(gxbh);
        MProcedureModule mProcedureModule=new MProcedureModule();
        int insertmpm=0;
        for(int i=0;i<mDesignProcedureModuleDetails.size();i++){
            mProcedureModule.setParentId(gxbh);
            mProcedureModule.setDetailsInt(mDesignProcedureModuleDetails.get(i).getDetailsInt());
            mProcedureModule.setParentId(mDesignProcedureModuleDetails.get(i).getParentId()+"");
            mProcedureModule.setProductId(mDesignProcedureModuleDetails.get(i).getProductId());
            mProcedureModule.setProductName(mDesignProcedureModuleDetails.get(i).getProductName());
            mProcedureModule.setCostPrice(mDesignProcedureModuleDetails.get(i).getCostPrice());
            mProcedureModule.setAmount(mDesignProcedureModuleDetails.get(i).getAmount());
            mProcedureModule.setSubtotal(mDesignProcedureModuleDetails.get(i).getSubtotal());
            insertmpm = yyDfileService.insertmpm(mProcedureModule);
        }
        return insertmpm;
    }
    @RequestMapping("/AddMf")
    @ResponseBody
    public int AddMf(String cpid,String paigd,String name,String amount,
                     String ms,String gdr,String bz,String sj,String djr,String war) throws ParseException {
        MManufacture mManufacture=new MManufacture();
        String c = yyDfileService.selid();
        int substring2 = Integer.parseInt(c.substring(11, 15));
        int i = substring2+ 10000 + 1;
        String ss = String.valueOf(i);
        String lsh = ss.substring(1,5);
        Date date= new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String xx = sdf.format(date);
        mManufacture.setManufactureId("301"+xx+""+lsh+"");
        mManufacture.setProductId(cpid);
        mManufacture.setProductName(name);
        mManufacture.setAmount(Integer.parseInt(amount));
        mManufacture.setProductDescribe(ms);
        mManufacture.setLabourCostPriceSum(Integer.parseInt(war));
        mManufacture.setRegister(djr);
        mManufacture.setDesigner(gdr);
        mManufacture.setRemark(bz);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = simpleDateFormat.parse(sj);
        mManufacture.setRegisterTime(parse);
        mManufacture.setCheckTag("S001-0");
        mManufacture.setManufactureProcedureTag("S002-0");
        mManufacture.setModuleCostPriceSum(allsutoalW);
        return yyDfileService.insertMf(mManufacture);
    }

    @RequestMapping("/allShDan")
    @ResponseBody
    public PageVo<MManufacture> allGxu(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String num = map.get("num");
        String cha = map.get("cha");
        return yyDfileService.allShdan(page,num,cha);
    }

    @RequestMapping("/chaShDan")
    @ResponseBody
    public MManufacture chaShDan(String manufactureId){
        return yyDfileService.chaShdan(manufactureId);
    }
    @Interceptor(methodName = "审核派工单")
    @RequestMapping("/updataMf")
    @ResponseBody
    public int updataMf(String wubh){
        return yyDfileService.ShenHe(wubh);
    }


    @PostMapping("/shenc")
    @ResponseBody
    public PageVo cha(@RequestBody Map<String,String> map){
        PageVo shenc = yyDfileService.shenc(Integer.valueOf(map.get("page")), Integer.valueOf(map.get("pageNum")));
        return shenc;
    }
    @PostMapping("/addjh")
    @ResponseBody
    public int addjh(@RequestBody MApply mApply){
        String s = yyDfileService.selId1();
        int substring = Integer.parseInt(s.substring(11, 15));
        int i = substring+ 10000 + 1;
        String ss = String.valueOf(i);
        String lsh = ss.substring(1,5);
        Date date= new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String xx = sdf.format(date);
        mApply.setApplyId("300"+xx+""+lsh+"");
        mApply.setCheckTag("S001-0");
        mApply.setManufactureTag("P001-0");
        int add = yyDfileService.add(mApply);
        return add;
    }
    @PostMapping("/chajh")
    @ResponseBody
    public PageVo cha111(@RequestBody Map<String,String> map){
        PageVo shenc = yyDfileService.chajh1(Integer.valueOf(map.get("page")), Integer.valueOf(map.get("pageNum")));
        return shenc;
    }
    @GetMapping(value = "sh1/{applyId}")
    @ResponseBody
    public List<MApply> chash1(@PathVariable("applyId") String applyId){
        List<MApply> mApplies = yyDfileService.shjh1(applyId);
        return mApplies;
    }
    @PostMapping("/xgjh")
    @ResponseBody
    public int update111(@RequestBody MApply mApply){
        int update = yyDfileService.update(mApply);
        return update;
    }
    @PostMapping("/chasy")
    @ResponseBody
    public PageVo ewqeq(@RequestBody Map<String,String> map,MApply mApply){
        System.out.println(map);
        String applyId = map.get("applyId");
        String productName = map.get("productName");
        String checkTag = map.get("checkTag");
        String register_time1 = map.get("registerTime1");
        String register_time2 = map.get("registerTime2");
        mApply.setApplyId(applyId);
        mApply.setProductName(productName);
        mApply.setCheckTag(checkTag);
        mApply.setRegisterTime1(register_time1);
        mApply.setRegisterTime2(register_time2);
        PageVo shenc = yyDfileService.chasy(Integer.valueOf(map.get("page")), Integer.valueOf(map.get("pageNum")),mApply);
        return shenc;
    }
    @PostMapping("/nbcha")
    @ResponseBody
    public PageVo nbcha(@RequestBody Map<String,String> map){
        PageVo chanb = yyDfileService.chanb(Integer.valueOf(map.get("page")), Integer.valueOf(map.get("pageNum")));
        System.out.println(chanb);
        return chanb;
    }

    @PostMapping("/chanbsy")
    @ResponseBody
    public PageVo chanbsy(@RequestBody Map<String,String> map,MManufacture mManufacture){
        String manufactureId = map.get("manufactureId");
        String productName = map.get("productName");
        String checkTag = map.get("checkTag");
        String register_time1 = map.get("registerTime1");
        String register_time2 = map.get("registerTime2");
        mManufacture.setManufactureId(manufactureId);
        mManufacture.setProductName(productName);
        mManufacture.setCheckTag(checkTag);
        mManufacture.setRegisterTime1(register_time1);
        mManufacture.setRegisterTime2(register_time2);
        PageVo chanbsy = yyDfileService.chanbsy(Integer.valueOf(map.get("page")), Integer.valueOf(map.get("pageNum")),mManufacture);
        return chanbsy;
    }
    @GetMapping(value = "chadj/{manufactureId}")
    @ResponseBody
    public MManufacture chadj(@PathVariable("manufactureId") String manufactureId){
        MManufacture chadj = yyDfileService.chadj(manufactureId);
        return chadj;
    }
    @GetMapping(value = "chagx/{parentId}")
    @ResponseBody
    public List<MProcedure> chagx(@PathVariable("parentId") String parentId){
        List<MProcedure> chagx = yyDfileService.chagx(parentId);
        return  chagx;
    }
    @GetMapping("/djc")
    @ResponseBody
    public List<MProcedure> djc(String detailsInt, String parentId){
        List<MProcedure> djc = yyDfileService.djc(detailsInt,parentId);
        return djc;
    }
    @PostMapping("/xggx")
    @ResponseBody
    public int xgzb(@RequestBody MProcedure mProcedure){
        System.out.println(mProcedure);
        int xgsczb = yyDfileService.xgsczb(mProcedure);
        return xgsczb;
    }
    @PostMapping("/addjl")
    @ResponseBody
    public int addjl(@RequestBody MProceduring mProceduring){
        int add = yyDfileService.add(mProceduring);
        return add;
    }
    @PostMapping("/addwljl")
    @ResponseBody
    public int addwljl(@RequestBody MProcedureModuling mProcedureModuling){
        System.out.println(mProcedureModuling);
        int add = yyDfileService.add(mProcedureModuling);
        return add;
    }
    @PostMapping("/fh1")
    @ResponseBody
    public PageVo chafuhe(@RequestBody Map<String,String> map){
        PageVo chafh = yyDfileService.chafh(Integer.valueOf(map.get("page")), Integer.valueOf(map.get("pageNum")));
        return chafh;
    }
    @GetMapping(value = "fhdj/{manufactureId}")
    @ResponseBody
    public MManufacture fuhedj(@PathVariable("manufactureId") String manufactureId){
        MManufacture chadj = yyDfileService.fhdj(manufactureId);
        return chadj;
    }
    @GetMapping(value = "chafhgx/{parentId}")
    @ResponseBody
    public List<MProcedure> chafhgx(@PathVariable("parentId") String parentId){
        List<MProcedure> chagx = yyDfileService.chafhgx(parentId);
        return  chagx;
    }
    @GetMapping("/fuhecha")
    @ResponseBody
    public List<MProceduring> fuhecha(String detailsInt, String parentId){
        List<MProceduring> fhc = yyDfileService.fuhecha(detailsInt,parentId);
        return fhc;
    }
    @PostMapping("/fhtg")
    @ResponseBody
    public int fhtg(@RequestBody MProcedure mProcedure){
        System.out.println(mProcedure);
        int fhtg = yyDfileService.fhtg(mProcedure);
        return fhtg;
    }
    @PostMapping("/gxsl")
    @ResponseBody
    public int gxsl(Integer detailsInt,String parentId,Integer realAmount){
        int gxsl = yyDfileService.gxsl(detailsInt,parentId,realAmount);
        return gxsl;
    }
    @PostMapping("/gxslfh")
    @ResponseBody
    public int gxslfh(Integer detailsInt,String parentId){
        int gxslfh = yyDfileService.gxslfh(detailsInt,parentId);
        return gxslfh;
    }
    @PostMapping(value = "xgzt/{manufactureId}")
    @ResponseBody
    public int xgzt(@PathVariable("manufactureId")String manufactureId){
        System.out.println(manufactureId);
        int xgzt = yyDfileService.xgzt(manufactureId);
        return xgzt;
    }
}
